package com.horay.statemachiedemo.multi.controller;

import com.horay.statemachiedemo.multi.enums.FormState;
import com.horay.statemachiedemo.multi.event.FormEvent;
import com.horay.statemachiedemo.multi.model.Form;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@RestController
@RequestMapping("factory")
public class FactoryController {

    private final StateMachineFactory<FormState, FormEvent> stateMachineFactory;

    private final StateMachinePersister<FormState, FormEvent, String> formStateMachinePersister;

    public FactoryController(StateMachineFactory<FormState, FormEvent> stateMachineFactory, StateMachinePersister<FormState, FormEvent, String> formStateMachinePersister) {
        this.stateMachineFactory = stateMachineFactory;
        this.formStateMachinePersister = formStateMachinePersister;
    }

    @PostMapping("/form/write")
    public String write(String formId) throws Exception {

        StateMachine<FormState, FormEvent> stateMachine = stateMachineFactory.getStateMachine();
        // 创建流程
        stateMachine.start();
        // 触发WRITE事件
        Form form = new Form(formId, "WRITE-547568678", "WRITE-广东省深圳市");
        Message<FormEvent> message = MessageBuilder.withPayload(FormEvent.WRITE).setHeader(Form.class.getName(), form).build();
        stateMachine.sendEvent(message);
        // 获取状态
        log.info("FormId:[{}], State:[{}]", formId, stateMachine.getState().getId());
        formStateMachinePersister.persist(stateMachine, form.getFormId());
        return "SUCCESSS";

    }

    @PostMapping("/form/confirm")
    public String confirm(String formId) throws Exception {

        StateMachine<FormState, FormEvent> stateMachine = stateMachineFactory.getStateMachine();
        stateMachine = formStateMachinePersister.restore(stateMachine, formId);
        // 触发CONFIRM事件
        Form form = new Form(formId, "CONFIRM-547568678", "CONFIRM-广东省深圳市");
        Message<FormEvent> message = MessageBuilder.withPayload(FormEvent.CONFIRM).setHeader(Form.class.getName(), form).build();
        stateMachine.sendEvent(message);
        // 获取状态
        log.info("FormId:[{}], State:[{}]", formId, stateMachine.getState().getId());
        formStateMachinePersister.persist(stateMachine, form.getFormId());
        return "SUCCESSS";

    }

    @PostMapping("/form/submit")
    public String submit(String formId) throws Exception {

        StateMachine<FormState, FormEvent> stateMachine = stateMachineFactory.getStateMachine();
        stateMachine = formStateMachinePersister.restore(stateMachine, formId);
        // 触发SUBMIT事件
        Form form = new Form(formId, "SUBMIT-547568678", "SUBMIT-广东省深圳市");
        Message<FormEvent> message = MessageBuilder.withPayload(FormEvent.SUBMIT).setHeader(Form.class.getName(), form).build();
        stateMachine.sendEvent(message);
        // 获取状态
        log.info("FormId:[{}], State:[{}]", formId, stateMachine.getState().getId());
        formStateMachinePersister.persist(stateMachine, form.getFormId());
        return "SUCCESSS";

    }

}
